package com.wuye.controller;

import com.wuye.model.UserAddress;
import com.wuye.model.repair.Category;
import com.wuye.model.repair.RepairOrder;
import com.wuye.model.repair.RepairPerson;
import com.wuye.model.repair.RepairState;
import com.wuye.repository.CategoryRepository;
import com.wuye.repository.RepairOrderRepository;
import com.wuye.repository.RepairPersonRepository;
import com.wuye.repository.UserAddressRepository;
import com.wuye.web.Pagination;
import com.wuye.web.ST;
import com.wuye.web.SessionHandler;
import com.wuye.web.UserSession;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by jiawenlong on 2016/7/26.
 */
@Controller
@RequestMapping("/repair")
public class RepairController extends BaseController {
    private Log log = LogFactory.getLog(this.getClass());
    @Autowired
    private SessionHandler sessionHandler;
    @Autowired
    CategoryRepository categoryRepository;
    @Autowired
    RepairPersonRepository repairPersonRepository;
    @Autowired
    RepairOrderRepository repairOrderRepository;
    @Autowired
    UserAddressRepository userAddressRepository;

    /**
     * 填写报修信息
     */
    @RequestMapping("repairOrder.jspa")
    public String repairOrder(Model model,HttpSession session) {
        UserSession userSession = sessionHandler.getUserSession(session);
        List<UserAddress> as = userAddressRepository.findByUserId(userSession.getUserId());
        if(as.size()>0){
            model.addAttribute("userAddress",as.get(0));
        }
        return "/jspa/repair/repairOrder";
    }
    @ResponseBody
    @RequestMapping("userAddress.json")
    public String userAddress(Model model,HttpSession session) {
        UserSession userSession = sessionHandler.getUserSession(session);
        List<UserAddress> as = userAddressRepository.findByUserId(userSession.getUserId());
        return ST.ok.getJsonObject(as).toString();
    }
    @ResponseBody
    @RequestMapping("saveUserAddress.json")
    public String saveUserAddress(Model model, HttpSession session,
                                  @RequestParam(value = "addrId", defaultValue = "-1")long addrId,
                                  @RequestParam(value = "name", defaultValue = "")String name,
                                  @RequestParam(value = "phone", defaultValue = "")String phone,
                                  @RequestParam(value = "address", defaultValue = "")String address
                                  ) {
        UserAddress userAddress = userAddressRepository.findOne(addrId);
        UserSession userSession = sessionHandler.getUserSession(session);
        if(null == userAddress){
            userAddress = new UserAddress();
        }
        userAddress.setUserId(userSession.getUserId());
        userAddress.setName(name);
        userAddress.setPhone(phone);
        userAddress.setAddress(address);
        userAddressRepository.save(userAddress);
        return ST.ok.toJson();
    }

    /**
     * 管理报修订单
     */
    @ResponseBody
    @RequestMapping("orderList.json")
    public String manageRepairOrder(HttpServletRequest request,HttpSession session, Model model ) throws ParseException {
        UserSession userSession = sessionHandler.getUserSession(session);
        List<Object[]> orders = repairOrderRepository.findByCreater(userSession.getUserId());
        org.json.JSONObject jo = ST.ok.getJsonObject(orders);
        RepairState[] states = RepairState.values();
        JSONObject object = new JSONObject();
        for (RepairState state : states) {
            object.put(state.state, state.getStateDesc());
        }
        jo.put("state",object);
        return jo.toString();
    }



}
